package com.molyfun.parents.activity.record.itemview;

import android.content.Context;
import android.graphics.Color;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import com.makeramen.roundedimageview.RoundedTransformationBuilder;
import com.molyfun.parents.MFPApplication;
import com.molyfun.parents.R;
import com.molyfun.parents.activity.common.ImageViewerActivity;
import com.molyfun.parents.rest.QiniuSDK;
import com.molyfun.parents.rest.bean.RecordResponse;
import com.molyfun.parents.rest.bean.StringResponse;
import com.molyfun.parents.rest.bean.UploadUrlRequest;
import com.molyfun.parents.utils.UrlUtils;
import com.squareup.picasso.Picasso;
import com.squareup.picasso.Transformation;

import butterknife.ButterKnife;
import butterknife.InjectView;
import cn.gavinliu.android.lib.shapedimageview.ShapedImageView;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Created by william(liuchao.hit@gmail.com).
 * Date:11/3/16.
 * <p>
 * 此类为容器
 */
public class RecordImageView extends BaseRecordView {

    protected LayoutInflater layoutInflater;

    private Context context;

    @InjectView(R.id.record_img)
    ShapedImageView mRecordImg;
    @InjectView(R.id.record_user)
    TextView mRecordUser;
    @InjectView(R.id.course_name)
    TextView mCourseName;

    Transformation transformation = new RoundedTransformationBuilder()
            .borderColor(Color.TRANSPARENT)
            .borderWidthDp(6)
            .oval(false)
            .build();

    public RecordImageView(Context context, AttributeSet attrs, int defStyle) {
        this(context, attrs);
    }

    public RecordImageView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs);
    }

    public RecordImageView(Context context) {
        super(context);
        init(context, null);
    }

    private void init(Context context, AttributeSet attrs) {
        this.context = context;
        layoutInflater = LayoutInflater.from(context);
        layoutInflater.inflate(R.layout.widget_record_image, this);
        ButterKnife.inject(this);
    }

    /**
     * init view
     */
    public void initData(RecordResponse.RecordBean recordBean) {
        if (recordBean != null) {
            mRecordBean = recordBean;
            if (!TextUtils.isEmpty(recordBean.getUrl())) {


                if(UrlUtils.isExpired(recordBean.getUrl())){
                    UploadUrlRequest uploadUrlRequest = new UploadUrlRequest();
                    uploadUrlRequest.setUrl(UrlUtils.getNewUrl(recordBean.getUrl()));
                    Call<StringResponse> call = MFPApplication.getInstance().getRestClient().updateMediaUrl(uploadUrlRequest);
                    call.enqueue(new Callback<StringResponse>() {
                        @Override
                        public void onResponse(Call<StringResponse> call, Response<StringResponse> response) {
                            mRecordBean.setUrl(response.body().getData());
                            Picasso.with(context)
                                    .load(response.body().getData())
                                    .transform(transformation)
                                    .placeholder(R.mipmap.ic_main_side_header_bg)
                                    .error(R.mipmap.ic_main_side_header_bg)
                                    .into(mRecordImg);
                        }

                        @Override
                        public void onFailure(Call<StringResponse> call, Throwable t) {
                            mRecordImg.setImageResource(R.mipmap.ic_main_side_header_bg);
                        }
                    });
                }else{
                    Picasso.with(context)
                            .load(QiniuSDK.getBigUrl(recordBean.getUrl()))
                            .placeholder(R.mipmap.ic_main_side_header_bg)
                            .into(mRecordImg);
                }

            } else {
                mRecordImg.setImageResource(R.mipmap.ic_main_side_header_bg);
            }
            mRecordUser.setText(mRecordBean.getUsername());
            mCourseName.setText(recordBean.getCoursename());
            mRecordImg.setOnClickListener(this);
            mRecordImg.setOnLongClickListener(this);
        }
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {
            case R.id.record_img:
                ImageViewerActivity.startActivity(context, QiniuSDK.getBigUrl(mRecordBean.getUrl()));
                break;
        }
    }

}